package com.GlobalPaint.app.Base;

/**
 * Created by Administrator on 2016/6/13.
 */

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;

import java.util.List;

public class BaseCommonAdapter<T> extends BaseAdapter {
    protected Context context;
    protected int layout_resource_id;
    protected List<T> adapter_data;
    protected int m_max_item_count;
    protected int m_first_item_position;
    protected int m_selected_position;
    protected String m_extra_prefix = "";

    public BaseCommonAdapter(Context context, int layout_resource_id, List<T> adapter_data, int max_item_count) {
        super();
        this.context = context;
        this.layout_resource_id = layout_resource_id;
        this.adapter_data = adapter_data;
        m_max_item_count = max_item_count;
        m_selected_position = -1;
        m_first_item_position = 0;
    }

    public void setExtraPrefix(String extra_prefix) {
        m_extra_prefix = extra_prefix;
    }

    public void setSelectedItem(int selected_position) {
        this.m_selected_position = m_first_item_position + selected_position;
    }

    public void setSelectedItem(T selected_object) {
        m_selected_position = adapter_data.indexOf(selected_object);
        m_selected_position = m_selected_position != -1 ? m_selected_position : 0;
    }

    @Override
    public int getCount() {
        if (adapter_data == null) return 0;
        int total_item_data_count = adapter_data.size();
        return total_item_data_count <= m_max_item_count ? total_item_data_count : m_max_item_count;
    }
    @Override
    public Object getItem(int arg0) {

        return adapter_data.get(m_first_item_position + arg0);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    //该方法需要进一步去实现
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        RelativeLayout list_item_layout = (RelativeLayout) LayoutInflater.from(context).inflate(layout_resource_id, null);
        return list_item_layout;
    }
}
